package com.hxtec.polaris.controller;

import com.hxtec.polaris.commons.MapperUtils;
import com.hxtec.polaris.commons.api.vo.Result;
import com.hxtec.polaris.entity.ShopProductSaleAttr;
import com.hxtec.polaris.entity.ShopProductVariant;
import com.hxtec.polaris.entity.ShopProductVariantAttrValue;
import com.hxtec.polaris.entity.ShopStock;
import com.hxtec.polaris.service.ProductService;
import com.hxtec.polaris.service.StockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Caps.Xia
 */
@RestController
@RequestMapping("product")
@CrossOrigin
@Api(value = "/product", tags = "商品详情")
public class ProductController {

    private final ProductService skuService;

    private final StockService shopStockService;

    public ProductController(ProductService skuService, StockService shopStockService) {
        this.skuService = skuService;
        this.shopStockService = shopStockService;
    }


    /**
     * 获取商品详情
     * @return
     * @param sku
     */
    @ApiOperation(value = "根据sku获取商品详情")
    @GetMapping(value = "getProductBySku/{sku}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sku", value = "商品的sku", required = true, dataType = "String"),
    })
    public Result<?> getProductBySku(@PathVariable int sku, HttpServletRequest request){
        return Result.ok(skuService.getProductDetail(request,sku));
    }


    @ApiOperation(value="保存商品信息", notes="商保存商品信息")
    @PostMapping("saveSkuInfo")
    public Result<?> saveSkuInfo(@RequestBody ShopProductVariant skuInfo){
        // 处理默认图片
        String skuDefaultImg = skuInfo.getImage();
        if(StringUtils.isBlank(skuDefaultImg)){
            skuInfo.setImage(skuInfo.getSkuImageList().get(0).getImgUrl());
        }
        skuService.saveSkuInfo(skuInfo);
        return Result.ok("success");
    }

}
